import unittest
from modules import CompressionModule, DeduplicationModule, EncryptionModule
import os

class TestCompression(unittest.TestCase):
    def test_gzip(self):
        data = os.urandom(1024*1024)  # 1MB随机数据
        cm = CompressionModule('gzip')
        compressed = cm.compress(data)
        self.assertLess(len(compressed), len(data))
        self.assertEqual(cm.decompress(compressed), data)

class TestDeduplication(unittest.TestCase):
    def test_dedup(self):
        dedup = DeduplicationModule(':memory:')
        block = os.urandom(4096)
        blocks = [block, block, os.urandom(4096)]
        unique, rate = dedup.process_blocks(blocks)
        self.assertEqual(len(unique), 2)
        self.assertAlmostEqual(rate, 33.33, delta=0.1)

class TestEncryption(unittest.TestCase):
    def test_aes(self):
        key = os.urandom(32)
        data = b"Important database content"
        em = EncryptionModule(key)
        encrypted = em.encrypt(data)
        self.assertEqual(em.decrypt(encrypted), data)